package com.duobaoyu.dby.supply.admin.controller.risk;

import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.validator.ValidationUtils;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.admin.converter.SupplyChainAdminConverter;
import com.duobaoyu.dby.supply.biz.service.config.PreWarningConfigService;
import com.duobaoyu.dby.supply.common.biz.bo.config.PreWarningConfigGroupBO;
import com.duobaoyu.dby.supply.common.biz.bo.config.PreWarningConfigSelectBO;
import com.duobaoyu.dby.supply.common.biz.bo.config.PreWarningReminderConfigBO;
import com.duobaoyu.dby.supply.common.biz.dto.config.PreWarningUpdateDTO;
import com.duobaoyu.dby.supply.common.biz.vo.config.PreWarningConfigGroupVO;
import com.duobaoyu.dby.supply.common.biz.vo.config.PreWarningConfigSelectVO;
import com.duobaoyu.dby.supply.common.biz.vo.config.PreWarningConfigVO;
import com.duobaoyu.dby.supply.common.biz.vo.config.PreWarningReminderConfigVO;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import com.duobaoyu.dby.supply.db.entity.config.PreWarningConfig;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 预警配置
 * @author chenms
 * @date 2023/8/16 6:01 PM
 */
@RestController
@RequestMapping("/admin/preWarning/config")
@RequiredArgsConstructor
public class PreWarningController {

    private final PreWarningConfigService preWarningConfigService;
    private final SupplyChainAdminConverter supplyChainAdminConverter;

    /**
     * 预警配置修改
     * @param preWarningUpdate
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "预警配置修改", notes = "预警配置修改")
    @SupplySysLog(value = "预警配置修改",operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> updatePreWarningConfig(@RequestBody @Valid PreWarningUpdateDTO preWarningUpdate) {
        Optional.ofNullable(preWarningUpdate.getMobiles()).ifPresent(mobiles ->
                mobiles.forEach(mobile -> CustomAssert.isTrue(ValidationUtils.isPhone(mobile), "手机号码不正确")));
        return R.ok(preWarningConfigService.updatePreWarningConfig(preWarningUpdate));
    }

    /**
     * 预警分组配置
     * @return
     */
    @GetMapping("/groupInfo")
    @ApiOperation(value = "预警分组配置", notes = "预警分组配置")
    @SupplySysLog(value = "预警分组配置",operationType = OperationTypeEnum.SELECT)
    public R<List<PreWarningConfigGroupVO>> getPreWarningConfigGroupInfo() {
        List<PreWarningConfigGroupBO> preWarningConfigGroup = preWarningConfigService.listPreWarningConfigGroup();
        return R.ok(supplyChainAdminConverter.toPreWarningConfigGroupList(preWarningConfigGroup));
    }

    /**
     * 预警查询条件下拉列表数据
     * @param scene 预警场景:1-实物订单列表 2-虚拟订单列表 3-订单列表 4-售后列表 8-异常订单
     * @return
     */
    @GetMapping("/select")
    @ApiOperation(value = "预警查询条件下拉列表数据", notes = "预警查询条件下拉列表数据")
    @SupplySysLog(value = "预警查询条件下拉列表数据",operationType = OperationTypeEnum.SELECT)
    public R<List<PreWarningConfigSelectVO>> getPreWarningSelectData(Integer scene) {
        List<PreWarningConfigSelectBO> preWarningConfigSelects = preWarningConfigService.getPreWarningSelectData(scene);
        return R.ok(supplyChainAdminConverter.toPreWarningConfigSelectList(preWarningConfigSelects));
    }

    /**
     * 获取配置明细
     * @param id 配置id
     * @return
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取配置明细", notes = "获取配置明细")
    @SupplySysLog(value = "获取配置明细",operationType = OperationTypeEnum.SELECT)
    public R<PreWarningConfigVO> updatePreWarningConfig(@PathVariable Long id) {
        PreWarningConfig preWarningConfig = preWarningConfigService.getPreWarningConfig(id);
        return R.ok(supplyChainAdminConverter.toPreWarningConfig(preWarningConfig));
    }

    /**
     * 预警提醒信息
     * @return
     */
    @GetMapping("/reminderInfo")
    @ApiOperation(value = "预警提醒信息", notes = "预警提醒信息")
    @SupplySysLog(value = "预警提醒信息",operationType = OperationTypeEnum.SELECT)
    public R<PreWarningReminderConfigVO> getPreWarningReminderConfig(Integer groupCode){
        PreWarningReminderConfigBO preWarningReminderConfig = preWarningConfigService.getPreWarningReminderConfig(groupCode);
        return R.ok(supplyChainAdminConverter.toPreWarningReminderConfig(preWarningReminderConfig));
    }

}
